package com.kvin.unionlib;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;

import com.kvin.paylib.R;
import com.unionpay.UPPayAssistEx;

/**
 * Created by Kvin on 2017/6/12.
 */
public class UnionUtils {
    //plugin params
    public static final int PLUGIN_NOT_INSTALLED = -1;
    public static final int PLUGIN_NEED_UPGRADE = 2;
    //mode
    public static final String ENVI_PUBLISH = "00";
    public static final String ENVI_DEBUG = "01";
    private AlertDialog.Builder builder;

    private Activity act;


    private UnionUtils(Activity act){
        this.act=act;
    }

    public static UnionUtils getInstance(Activity act){
        return new UnionUtils(act);
    }

    /**
     * start pay
     * mode 00 启动银联正式环境;01 连接银联测试环境
     */
    public int unionPay(String tn, String mode) {
        int ret = UPPayAssistEx.startPay(act, null, null, tn, mode);
        Log.e("", "" + ret);
        return ret;
    }

    /**
     * show default dialog
     */
    public void showPluginDialog(String title, String msg, DialogInterface.OnClickListener cancelListener, DialogInterface.OnClickListener submitListener) {
        builder = new AlertDialog.Builder(act);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setNegativeButton(R.string.dialog_cancel, cancelListener == null ? getDefaultCancelListener() : cancelListener);
        builder.setNegativeButton(R.string.dialog_confirm, cancelListener == null ? getDefaultConfirmListener() : cancelListener);
        builder.create().show();
    }

    /**
     * default cancel click listener
     */
    private DialogInterface.OnClickListener getDefaultCancelListener() {
        return new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        };
    }

    /**
     * default confirm click listener
     */
    private DialogInterface.OnClickListener getDefaultConfirmListener() {
        return new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                UPPayAssistEx.installUPPayPlugin(act);
                dialog.dismiss();
            }
        };
    }


    /**
     * verify sign
     */
    public boolean verifySign(String msg, String sign, String mode) {
        //you`d better complete it in web server
        return true;
    }


    /**
     * response tag;
     */
    public class Tag {
        public static final String PAY_RESULT = "pay_result";
        public static final String RESULT_DATA = "result_data";
        public static final String SIGN = "sign";
        public static final String DATA = "data";
        public static final String SUCCESS = "success";
        public static final String FAIL = "fail";
        public static final String CANCEL = "cancel";
    }

}
